#include<iostream>
#include <vcruntime.h>
#include<vector>
#include<unordered_map>
using namespace std;

class Solution0 {//暴力做法
public:
    int arithmeticTriplets(vector<int>& nums, int diff) {
        int ans=0;
        for(int i=0;i<nums.size()-2;i++)
        {
            for(int j=0;j<nums.size()-1;j++)
            {
                for(int k=0;k<nums.size();k++)
                {
                    if(nums[j]-nums[i]==diff&&nums[k]-nums[j]==diff)
                    {
                        ans++;
                    }
                }
            }
        }
        return ans;
    }
};


class Solution1 {//三指针做法
public:
    int arithmeticTriplets(vector<int>& nums, int diff) {
        int ans=0;
    
        int i=0,j=1;
        while(j<nums.size()-1)
        {
            while(i<j&&(nums[j]-nums[i])>diff)i++;
            if(nums[j]-nums[i]==diff)
            {
                int k=j+1;
                
                while(k<nums.size()&&(nums[k]-nums[j])<diff)k++;
                
                if(k<nums.size()&&nums[k]-nums[j]==diff)
                {
                    ans++;
                }
                else if(k==nums.size())return ans;

            }
            j++;
        }
        return ans;
    }
};

class Solution {//hash
public:
    int arithmeticTriplets(vector<int>& nums, int diff) {
        unordered_map<int,int>hash;
        int ans=0;
        for(auto num:nums)
        {
            if(hash.count(num-diff)&&hash.count(num-2*diff))ans++;
            hash[num]=1;
        }
        return ans;
    }
};